using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TinyERP.Framework;
using TinyERP.Framework.Controls;
using TinyERP.Framework.Validation;
using TinyERP.ERP.Business;
using TinyERP.ERP.DTO;
using log4net;
namespace TinyERP.View
{
    public class ProductEditInfoForm : BasicEditInfoDialog
    {
        private ILog logger = LogManager.GetLogger(typeof(ProductEditInfoForm));
		private ProductDTO product = new ProductDTO();
		private KLabel lblDescription;
		private KTextBox txtDescription;
		private KLabel lblName;
        private KTextBox txtName;
		private KLabel lblProducer;
		private TextBox txtProducer;
		private KLabel lblMaxUsageDate;
		private DateTextBox dtMaxUsageDate;
		private KLabel lblMeasurementUnitGroup;
        private ComboBox cbMeasurementUnitGroup;

		public ProductDTO Product
		{
			get 
			{
				return product;
			}
			set 
			{
				product = value;
				LoadEntityInfo();
			}
		}
		public IProductService ProductService 
		{
			get
			{
				return (IProductService) ServiceLocator.GetService("ProductServiceProxy");
			}
		}
		public IMeasurementUnitGroupService MeasurementUnitGroupService 
		{
			get 
			{
				return (IMeasurementUnitGroupService) ServiceLocator.GetService("MeasurementUnitGroupServiceProxy");
			}
		}	
		
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lblDescription = new TinyERP.Framework.Controls.KLabel();
            this.txtDescription = new TinyERP.Framework.Controls.KTextBox();
            this.lblName = new TinyERP.Framework.Controls.KLabel();
            this.txtName = new TinyERP.Framework.Controls.KTextBox();
            this.lblProducer = new TinyERP.Framework.Controls.KLabel();
            this.txtProducer = new System.Windows.Forms.TextBox();
            this.lblMaxUsageDate = new TinyERP.Framework.Controls.KLabel();
            this.dtMaxUsageDate = new TinyERP.Framework.Controls.DateTextBox();
            this.lblMeasurementUnitGroup = new TinyERP.Framework.Controls.KLabel();
            this.cbMeasurementUnitGroup = new System.Windows.Forms.ComboBox();
            this.contentPanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // contentPanel
            // 
            this.contentPanel.Controls.Add(this.lblDescription);
            this.contentPanel.Controls.Add(this.txtDescription);
            this.contentPanel.Controls.Add(this.lblName);
            this.contentPanel.Controls.Add(this.txtName);
            this.contentPanel.Controls.Add(this.lblProducer);
            this.contentPanel.Controls.Add(this.txtProducer);
            this.contentPanel.Controls.Add(this.lblMaxUsageDate);
            this.contentPanel.Controls.Add(this.dtMaxUsageDate);
            this.contentPanel.Controls.Add(this.lblMeasurementUnitGroup);
            this.contentPanel.Controls.Add(this.cbMeasurementUnitGroup);
            // 
            // lblDescription
            // 
            this.lblDescription.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.NormalControl;
            this.lblDescription.Location = new System.Drawing.Point(16, 16);
            this.lblDescription.Name = "lblDescription";
            this.lblDescription.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.lblDescription.ResourceKey = null;
            this.lblDescription.ResourceModuleKey = "Description, TinyERP.Core";
            this.lblDescription.Size = new System.Drawing.Size(73, 20);
            this.lblDescription.TabIndex = 0;
            this.lblDescription.Text = "Description";
            this.lblDescription.Values.ExtraText = "";
            this.lblDescription.Values.Image = null;
            this.lblDescription.Values.Text = "Description";
            // 
            // txtDescription
            // 
            this.txtDescription.InputControlStyle = ComponentFactory.Krypton.Toolkit.InputControlStyle.Standalone;
            this.txtDescription.Location = new System.Drawing.Point(126, 16);
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.txtDescription.Size = new System.Drawing.Size(266, 20);
            this.txtDescription.TabIndex = 1;
            // 
            // lblName
            // 
            this.lblName.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.NormalControl;
            this.lblName.Location = new System.Drawing.Point(16, 41);
            this.lblName.Name = "lblName";
            this.lblName.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.lblName.ResourceKey = null;
            this.lblName.ResourceModuleKey = "Name, TinyERP.Core";
            this.lblName.Size = new System.Drawing.Size(43, 20);
            this.lblName.TabIndex = 0;
            this.lblName.Text = "Name";
            this.lblName.Values.ExtraText = "";
            this.lblName.Values.Image = null;
            this.lblName.Values.Text = "Name";
            // 
            // txtName
            // 
            this.txtName.InputControlStyle = ComponentFactory.Krypton.Toolkit.InputControlStyle.Standalone;
            this.txtName.Location = new System.Drawing.Point(126, 41);
            this.txtName.Name = "txtName";
            this.txtName.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.txtName.Size = new System.Drawing.Size(266, 20);
            this.txtName.TabIndex = 2;
            // 
            // lblProducer
            // 
            this.lblProducer.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.NormalControl;
            this.lblProducer.Location = new System.Drawing.Point(16, 66);
            this.lblProducer.Name = "lblProducer";
            this.lblProducer.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.lblProducer.ResourceKey = null;
            this.lblProducer.ResourceModuleKey = "Producer, TinyERP.Core";
            this.lblProducer.Size = new System.Drawing.Size(60, 20);
            this.lblProducer.TabIndex = 0;
            this.lblProducer.Text = "Producer";
            this.lblProducer.Values.ExtraText = "";
            this.lblProducer.Values.Image = null;
            this.lblProducer.Values.Text = "Producer";
            // 
            // txtProducer
            // 
            this.txtProducer.Location = new System.Drawing.Point(126, 66);
            this.txtProducer.Name = "txtProducer";
            this.txtProducer.Size = new System.Drawing.Size(266, 20);
            this.txtProducer.TabIndex = 3;
            // 
            // lblMaxUsageDate
            // 
            this.lblMaxUsageDate.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.NormalControl;
            this.lblMaxUsageDate.Location = new System.Drawing.Point(16, 91);
            this.lblMaxUsageDate.Name = "lblMaxUsageDate";
            this.lblMaxUsageDate.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.lblMaxUsageDate.ResourceKey = null;
            this.lblMaxUsageDate.ResourceModuleKey = "MaxUsageDate, TinyERP.Core";
            this.lblMaxUsageDate.Size = new System.Drawing.Size(97, 20);
            this.lblMaxUsageDate.TabIndex = 0;
            this.lblMaxUsageDate.Text = "Max usage date";
            this.lblMaxUsageDate.Values.ExtraText = "";
            this.lblMaxUsageDate.Values.Image = null;
            this.lblMaxUsageDate.Values.Text = "Max usage date";
            // 
            // dtMaxUsageDate
            // 
            this.dtMaxUsageDate.Culture = new System.Globalization.CultureInfo("en-US");
            this.dtMaxUsageDate.DateFormat = null;
            this.dtMaxUsageDate.InputControlStyle = ComponentFactory.Krypton.Toolkit.InputControlStyle.Standalone;
            this.dtMaxUsageDate.Location = new System.Drawing.Point(126, 91);
            this.dtMaxUsageDate.Mask = "00/00/0000";
            this.dtMaxUsageDate.Name = "dtMaxUsageDate";
            this.dtMaxUsageDate.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.dtMaxUsageDate.Size = new System.Drawing.Size(266, 20);
            this.dtMaxUsageDate.TabIndex = 4;
            this.dtMaxUsageDate.Text = "  /  /";
            this.dtMaxUsageDate.Value = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
            // 
            // lblMeasurementUnitGroup
            // 
            this.lblMeasurementUnitGroup.LabelStyle = ComponentFactory.Krypton.Toolkit.LabelStyle.NormalControl;
            this.lblMeasurementUnitGroup.Location = new System.Drawing.Point(16, 116);
            this.lblMeasurementUnitGroup.Name = "lblMeasurementUnitGroup";
            this.lblMeasurementUnitGroup.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.lblMeasurementUnitGroup.ResourceKey = null;
            this.lblMeasurementUnitGroup.ResourceModuleKey = "MeasurementUnitGroup, TinyERP.Core";
            this.lblMeasurementUnitGroup.Size = new System.Drawing.Size(67, 20);
            this.lblMeasurementUnitGroup.TabIndex = 0;
            this.lblMeasurementUnitGroup.Text = "MU group";
            this.lblMeasurementUnitGroup.Values.ExtraText = "";
            this.lblMeasurementUnitGroup.Values.Image = null;
            this.lblMeasurementUnitGroup.Values.Text = "MU group";
            // 
            // cbMeasurementUnitGroup
            // 
            this.cbMeasurementUnitGroup.Location = new System.Drawing.Point(126, 116);
            this.cbMeasurementUnitGroup.Name = "cbMeasurementUnitGroup";
            this.cbMeasurementUnitGroup.Size = new System.Drawing.Size(266, 21);
            this.cbMeasurementUnitGroup.TabIndex = 5;
            // 
            // ProductEditInfoForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "ProductEditInfoForm";
            this.contentPanel.ResumeLayout(false);
            this.contentPanel.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion


		public ProductEditInfoForm() : base()
		{
			InitializeComponent();
			LoadEntityInfo();
		}
		
		protected override void AddValidationRules() 
		{
			base.AddValidationRules();
			//TODO: use validator to add validation rules on your input controls
			//Example: validator.AddValidationRule(new RequiredFieldRule(txtUserName, "User name"));
		}
		
		
		
		private void PrepareData() 
		{
			LoadDataOfMeasurementUnitGroup();
		}
		
		private void LoadDataOfMeasurementUnitGroup() 
		{
			long totalRecord = 0;
            IList measurementUnitGroupList = MeasurementUnitGroupService.GetAllMeasurementUnitGroup("ID", Bamboo.Core.Data.SortDirection.ASCENDING, out totalRecord);
            
            cbMeasurementUnitGroup.ValueMember = "ID";
            cbMeasurementUnitGroup.DisplayMember = "Name";
            cbMeasurementUnitGroup.DataSource = measurementUnitGroupList; 
		}
		protected override void LoadEntityInfo() 
		{
			PrepareData();
		    if(product==null) 
		    {
				product = new ProductDTO();
		    }
			txtDescription.Text = product.Description;
            txtName.Text = product.Name;
			txtProducer.Text = product.Producer;
			dtMaxUsageDate.Value = product.MaxUsageDate;
			cbMeasurementUnitGroup.SelectedValue = product.MeasurementUnitGroupID;
			deleteButton.Visible = (product.ID != 0) ;
		}
		
		protected override bool ValidateInputData() 
		{
			if(!base.ValidateInputData()) 
			{
				return false;
			}
			//TODO: add your business validation here
			return true;
		}
		
		private bool UpdateEntityInfoFromInput() 
		{
			if(!ValidateInputData()) 
			{
				return false;
			}
			product.Description = txtDescription.Text;
			product.Name = txtName.Text;
			product.Producer = txtProducer.Text;
			product.MaxUsageDate = dtMaxUsageDate.Value;
			if(cbMeasurementUnitGroup.SelectedValue != null) 
			{
				product.MeasurementUnitGroupID = (long) cbMeasurementUnitGroup.SelectedValue;
			}
			return true;
		}
        
        public override void OnSave()
        {
			if(!UpdateEntityInfoFromInput()) 
			{
				return;
			}
			try {
				if(product.ID==0) 
				{
					ProductService.SaveProduct(product);
				}
				else 
				{
					ProductService.UpdateProduct(product);
				}
				MessageBox.Show(CommonMessage.GetSaveSuccessMessage(GetEntityResourceKey()));
                InvokeOnNeedReloadData();
                InvokeCloseDialog();
			} 
			catch(Exception ex) 
			{
				logger.Error(ex);			
				MessageBox.Show(CommonMessage.GetCanNotSaveError(GetEntityResourceKey()));
			}
        }

        public override void OnDelete()
        {
			try 
			{
                if (ConfirmDelete() == DialogResult.OK)
                {
					ProductService.DeleteProductByID(product.ID);			
					MessageBox.Show(CommonMessage.GetDeleteSuccessMessage(GetEntityResourceKey()));
                    InvokeOnNeedReloadData();
                    InvokeCloseDialog();
				}
			}
			catch(Exception ex) 
			{
                logger.Error(ex);			
				MessageBox.Show(CommonMessage.GetCanNotDeleteError(GetEntityResourceKey()));
			}
        }

        
        public override string GetHeaderText()
        {
            return ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("ProductEditForm, TinyERP.Core");
        }

        private string GetEntityResourceKey()
        {
            return ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("Product, TinyERP.Core");
        }

        private DialogResult ConfirmDelete()
        {
            return MessageBox.Show(CommonMessage.GetDeleteConfirmMessage(GetEntityResourceKey()),
                CommonMessage.GetConfirmationText(), MessageBoxButtons.OKCancel);
        }

	}
}

